package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Comment;
import com.example.entity.Student;
import com.example.entity.Teacher;
import com.example.exception.CustomException;
import com.example.mapper.CommentMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评教信息表业务处理
 **/
@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

    /**
     * 新增
     */
    public void add(Comment comment) {
        // 判断该学生对该门课有没有进行评教过
        Comment dbComment = commentMapper.selectByTeacherAndCourseAndStudent(comment.getTeacher(), comment.getName(), comment.getStudent());
        if (ObjectUtil.isNotEmpty(dbComment)) {
            throw new CustomException(ResultCodeEnum.COMMENT_ALREADY_ERROR);
        }
        comment.setTime(DateUtil.now());
        commentMapper.insert(comment);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        commentMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        // 使用 Java 21 增强的 Stream API
        ids.forEach(commentMapper::deleteById);
    }
    /**
     * 修改
     */
    public void updateById(Comment comment) {
        commentMapper.updateById(comment);
    }

    /**
     * 根据ID查询
     */
    public Comment selectById(Integer id) {
        return commentMapper.selectById(id);
    }

    /**
     * 查询所有
     * 添加权限控制：学生只能查看自己的评教，教师只能查看对自己的评教
     */
    public List<Comment> selectAll(Comment comment) {
        List<Comment> list = commentMapper.selectAll(comment);
        
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            return list;
        }
        
        // 根据角色过滤评教数据
        return switch (currentUser.getRole()) {
            case "STUDENT" -> {
                // 学生只能看到自己的评教
                Student student = (Student) currentUser;
                yield list.stream()
                        .filter(c -> c.getStudent().equals(student.getName()))
                        .collect(Collectors.toList());
            }
            case "TEACHER" -> {
                // 教师只能看到对自己的评教
                Teacher teacher = (Teacher) currentUser;
                yield list.stream()
                        .filter(c -> c.getTeacher().equals(teacher.getName()))
                        .collect(Collectors.toList());
            }
            default -> list; // 管理员可以看到所有评教
        };
    }

    /**
     * 分页查询
     * 添加权限控制：学生只能查看自己的评教，教师只能查看对自己的评教
     */
    public PageInfo<Comment> selectPage(Comment comment, Integer pageNum, Integer pageSize) {
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null) {
            switch (currentUser.getRole()) {
                case "STUDENT" -> {
                    // 学生只能看到自己的评教
                    Student student = (Student) currentUser;
                    comment.setStudent(student.getName());
                }
                case "TEACHER" -> {
                    // 教师只能看到对自己的评教
                    Teacher teacher = (Teacher) currentUser;
                    comment.setTeacher(teacher.getName());
                }
                // 管理员不做限制
            }
        }
        
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> list = commentMapper.selectAll(comment);
        return PageInfo.of(list);
    }

}
